package util.javassist.hotswap;

import java.io.File;
import java.io.FileInputStream;

import javassist.util.HotSwapper;

public class Test {
	public static void main(String[] args) throws Exception {
		
		new HelloWorld().print();

		String oldHelloWorld = util.javassist.hotswap.logging.HelloWorld.class.getName();
		//System.out.println(oldHelloWorld);
		String classPath = ClassLoader.getSystemResource("").getPath().substring(1);
		//System.out.println(classPath);
		classPath += oldHelloWorld.replace('.', '/') + ".class";
		System.out.println(classPath);
		File newfile = new File(classPath);
		byte[] bytes = new byte[(int) newfile.length()];
		new FileInputStream(newfile).read(bytes);
		System.out.println("** reload a logging version");
		//System.out.println(new String(bytes));
		
		HotSwapper hs = new HotSwapper(8000);
		hs.reload(HelloWorld.class.getName(), bytes);
		new HelloWorld().print();

		String helloWorld = HelloWorld.class.getName();
		String classFilePath = ClassLoader.getSystemResource("").getPath().substring(1);
		classFilePath += helloWorld.replace('.', '/') + ".class";
		newfile = new File(classFilePath);
		bytes = new byte[(int) newfile.length()];
		new FileInputStream(newfile).read(bytes);
		System.out.println("** reload the original version");

		hs.reload(HelloWorld.class.getName(), bytes);
		new HelloWorld().print();
	}
}
